package edu.study.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.Page;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.PageRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.api.common.enums.BaseResultCodeEnum;
import edu.study.api.common.enums.StatusTypeEnum;
import edu.study.common.util.BeanCopy;
import edu.study.common.util.cache.Cache;
import edu.study.user.api.dto.DomainDto;
import edu.study.user.api.dto.UserDetailDto;
import edu.study.user.api.req.domain.AddDomainReq;
import edu.study.user.api.req.domain.DeleteDomainReq;
import edu.study.user.api.req.domain.ModifyDomainReq;
import edu.study.user.api.req.domain.ModifyDomainStatusReq;
import edu.study.user.api.req.domain.QueryDomainPageListReq;
import edu.study.user.api.req.domain.QueryDomainReq;
import edu.study.user.dao.mapperExt.DomainDOMapperExt;
import edu.study.user.dbBean.DomainDO;
import edu.study.user.util.UserUtils;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class DomainService {

	@Autowired
	private DomainDOMapperExt domainDOMapperExt;

	@Autowired
	private Cache cache;

	public BaseRespDto addDomain(AddDomainReq req) {
		BaseRespDto resp = new BaseRespDto();

		DomainDO domainDO = domainDOMapperExt.queryByDomainName(req.getDomainName());
		if (domainDO != null) {
			resp.setResultFail(BaseResultCodeEnum.RECORD_ALREADY_EXISTS);
			return resp;
		}
		DomainDO newDomainDO = new DomainDO();
		BeanCopy.copyFromSrcToDest(req, newDomainDO);
		String domainNo = cache.generateStr("domainNo_", 1, null);
		newDomainDO.setDomainNo(domainNo);
		UserDetailDto userDetailDto = UserUtils.getCurrentUser();
		if(userDetailDto!=null){
			newDomainDO.setCreator(userDetailDto.getLoginName());
			newDomainDO.setModifier(userDetailDto.getLoginName());
		}
		int i = domainDOMapperExt.insertSelective(newDomainDO);
		if (i == 0) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}

	public BaseRespDto deleteDomain(DeleteDomainReq req) {
		BaseRespDto resp = new BaseRespDto();

		DomainDO domainDO = domainDOMapperExt.queryByDomainNo(req.getDomainNo());
		if (domainDO == null) {
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		//todo 删除了，关联的数据怎么处理？？？
		DomainDO newDomainDO = new DomainDO();
		newDomainDO.setId(domainDO.getId());
		newDomainDO.setIsDeleted(StatusTypeEnum.Y.getCode());
		int i = domainDOMapperExt.updateByPrimaryKeySelective(newDomainDO);
		if (i == 0) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public BaseRespDto modifyDomain(ModifyDomainReq req) {
		BaseRespDto resp = new BaseRespDto();

		DomainDO domainDO = domainDOMapperExt.queryByDomainNo(req.getDomainNo());
		if (domainDO == null) {
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		DomainDO domainDO2 = domainDOMapperExt.queryByDomainName(req.getDomainName());
		if (domainDO2 != null && !domainDO2.getDomainNo().equals(req.getDomainNo())) {
			log.info("DomainService.modifyDomain, 不能修改,名称重复了, domainName={}", req.getDomainName());
			resp.setResultFail(BaseResultCodeEnum.RECORD_ALREADY_EXISTS);
			return resp;
		}
		DomainDO newDomainDO = new DomainDO();
		newDomainDO.setId(domainDO.getId());
		newDomainDO.setDomainName(req.getDomainName());
		newDomainDO.setDomainDesc(req.getDomainDesc());
		newDomainDO.setRemarks(req.getRemarks());
		UserDetailDto userDetailDto = UserUtils.getCurrentUser();
		if(userDetailDto!=null){
			newDomainDO.setModifier(userDetailDto.getLoginName());
		}
		int i = domainDOMapperExt.updateByPrimaryKeySelective(newDomainDO);
		if (i == 0) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public BaseRespDto modifyDomainStatus(ModifyDomainStatusReq req) {
		BaseRespDto resp = new BaseRespDto();

		DomainDO domainDO = domainDOMapperExt.queryByDomainNo(req.getDomainNo());
		if (domainDO == null) {
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		if(req.getStatus().equals(domainDO.getStatus())) {
			log.info("DomainService.modifyDomainStatus, 不用修改, domainNo={}", req.getDomainNo());
			return resp;
		}
		//todo 失效了，关联的数据怎么处理？？？
		DomainDO newDomainDO = new DomainDO();
		newDomainDO.setId(domainDO.getId());
		newDomainDO.setStatus(req.getStatus());
		UserDetailDto userDetailDto = UserUtils.getCurrentUser();
		if(userDetailDto!=null){
			newDomainDO.setModifier(userDetailDto.getLoginName());
		}
		int i = domainDOMapperExt.updateByPrimaryKeySelective(newDomainDO);
		if (i == 0) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public RespDto<DomainDto> queryDomain(QueryDomainReq req) {
		RespDto<DomainDto> resp = new RespDto<DomainDto>();

		DomainDO domainDO = domainDOMapperExt.queryByDomainNo(req.getDomainNo());
		if (domainDO == null) {
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		DomainDto domainDto = new DomainDto();
		BeanCopy.copyFromSrcToDest(domainDO, domainDto);
		resp.setData(domainDto);
		return resp;
	}
	
	public PageRespDto<DomainDto> queryDomainPageList(QueryDomainPageListReq req) {
		PageRespDto<DomainDto> resp = new PageRespDto<DomainDto>();
		Page<DomainDto> domainDtoPageList = domainDOMapperExt.queryDomainPageList(req);
		if (domainDtoPageList != null && !CollectionUtils.isEmpty(domainDtoPageList.getResult())) {
			resp.setPages(domainDtoPageList.getPages());
			resp.setTotal(domainDtoPageList.getTotal());
			resp.setData(domainDtoPageList.getResult());
		}
		return resp;
	}
}
